package com.wfit.controller;

import com.wfit.pojo.User;
import com.wfit.service.UserService;
import com.wfit.utils.JwtHelper;
import com.wfit.utils.Result;
import com.wfit.utils.ResultCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author 花满园
 * @date 2024/05/07 18:18
 * Description:
 */
@RestController
//返回json数据
@RequestMapping("user")
//以user开头 设置一个根路径
@CrossOrigin
//因为前端要访问 所以写一个跨域
public class UserController {
    @Autowired
    private JwtHelper jwtHelper;
    @Autowired
    private UserService userService;
    @PostMapping("login")
    public Result login(@RequestBody User user){
        Result result=userService.login(user);
        return result;
    }
    @GetMapping("getUserInfo")
    public Result getUserInfo(@RequestHeader String token){
        Result result=userService.getUserInfo(token);
        return result;
    }
    @PostMapping("checkUserName")
    public Result checkUserName(String username){
       Result result= userService.checkUserName(username);
       return result;
    }
    @PostMapping("regist")
    public Result regist(@RequestBody User user){
        Result result=userService.regist(user);
        return result;
    }
    @GetMapping("checkLogin")
    public  Result cheakLogin(@RequestHeader String token){
        boolean expiration = jwtHelper.isExpiration(token);
        if(expiration){
            return Result.build(null, ResultCodeEnum.NOTLOGIN);
        }
        return  Result.ok(null);
    }

}
